<template>
	<div class="system-role-container layout-padding">
		<div class="system-role-padding layout-padding-auto layout-padding-view">
			<div class="system-user-search mb15">
				<el-input size="default" class="w_130 mr10" v-model="listQuery.Name" placeholder="请输入用户名" @keydown.enter="searchForm"> </el-input>
				<el-input size="default" class="w_130 mr10" v-model="listQuery.AppKey" placeholder="请输入AppKey" @keydown.enter="searchForm"> </el-input>
				<el-input size="default" class="w_130 mr10" v-model="listQuery.Address" placeholder="请输入地址" @keydown.enter="searchForm"> </el-input>
				<el-select size="default" v-model="listQuery.Status" placeholder="状态" class="w_100" clearable @change="searchForm">
					<el-option label="开启" :value="0" />
					<el-option label="冻结" :value="1" />
				</el-select>
				<el-date-picker
					v-model="state.dateArr"
					type="daterange"
					size="default"
					range-separator="至"
					start-placeholder="开始日期"
					class="w_220 ml10"
					format="YYYY-MM-DD"
					value-format="YYYY-MM-DD"
					end-placeholder="结束日期"
					align="right"
					@change="searchForm"
				/>
				<el-button size="default" @click="searchForm" type="primary" class="ml10"> 查询 </el-button>
				<el-button size="default" type="info" class="ml10" @click="resetForm"> 重置 </el-button>
			</div>
			<tableTem ref="tableRef" v-bind:="state.tableData" @pageChange="onTablePageChange">
				<el-table-column align="center" type="index" label="序号" width="60" />
				<el-table-column
					v-for="(item, index) in state.tableTitle"
					:key="index"
					align="center"
					show-overflow-tooltip
					:prop="item.fieldName"
					min-width="120"
					:label="item.fieldTitle"
				>
					<template v-slot="scope">
						<template v-if="item.fieldTitle == '用户状态'">
							<el-tag type="success" v-if="scope.row.status == 1">启用</el-tag>
							<el-tag type="info" v-else>冻结</el-tag>
						</template>
						<template v-else>
							{{ scope.row[item.fieldName] }}
						</template>
					</template>
				</el-table-column>
				<el-table-column label="操作" align="center" width="150" fixed="right">
					<template #default="scope">
						<el-button size="small" type="primary" text @click="getDetail(scope.row)">详情</el-button>
					</template>
				</el-table-column>
			</tableTem>
		</div>
		<!-- 弹窗 -->
		<el-dialog
			:title="state.dialogTitle"
			v-model="state.dialogVisible"
			:close-on-click-modal="false"
			:show-close="false"
			:close-on-press-escape="false"
			:width="state.dialogWidth"
		>
			<dialogTem
				v-if="state.dialogType == 1"
				:list-item="state.tableTitle"
				:edit-item="state.editItem"
				:isDisabled="state.isDisabled"
				@close="closeDialog"
			/>
			<verifyTem />
		</el-dialog>
	</div>
</template>

<script setup lang="ts">
import { defineAsyncComponent, reactive, onMounted, ref } from 'vue';
import { UserGetPagedList, UserfieldGetList } from '/@/api/userInformation/index';

// 引入组件
const dialogTem = defineAsyncComponent(() => import('./component/dialog.vue'));
const verifyTem = defineAsyncComponent(() => import('./component/verify.vue'));
const tableTem = defineAsyncComponent(() => import('/@/components/tables/index.vue'));

// 定义变量内容
const tableRef = ref();
const listQuery = reactive({
	Name: '',
	AppKey: '', // AppKey
	Address: '', // 地址
	Status: '', // 状态
});
const state = reactive<any>({
	tableData: {
		data: [],
		total: 0,
		loading: true,
		param: {
			PageIndex: 1,
			PageSize: 10,
		},
	},
	tableTitle: [], // 列表标题
	editItem: {},
	isDisabled: false, // 详情
	dialogType: 0, // 弹窗类型
	dialogTitle: '', // 弹窗标题
	dialogWidth: '', // 弹窗标题
	dialogVisible: false, // 弹窗状态
	dateArr: [],
});
// 页面加载时
onMounted(() => {
	getTableData();
});

// 初始化表格数据
const getTableData = async () => {
	state.tableData.loading = true;
	try {
		const params = {
			...state.tableData.param,
			...listQuery,
		};
		if (Array.isArray(state.dateArr) && state.dateArr.length > 0) {
			params.StartTime = `${state.dateArr[0]} 00:00:00`;
			params.EndTime = `${state.dateArr[0]} 23:59:59`;
		}

		Object.keys(params).forEach((key) => {
			if (!params[key]) delete params[key];
		});
		let dataTitle = await UserfieldGetList();
		let data = await UserGetPagedList(params);

		console.log(data.data, 4343);
		if (data.data == false) return openDialog('安全验证', 2, '380px');

		state.tableTitle = dataTitle.data;

		state.tableData.data = data.data.items;
		state.tableData.total = data.data.totalCount;
	} finally {
		state.tableData.loading = false;
	}
};
// 弹窗打开
const openDialog = (title: any, type: any, width: any) => {
	state.dialogTitle = title;
	state.dialogType = type;
	state.dialogVisible = true;
	state.dialogWidth = width;
};

// 详情
const getDetail = (v: any) => {
	const labelsWithValues = state.tableTitle.reduce((acc: any, item: any) => {
		acc[item.fieldName] = v[item.fieldName];
		return acc;
	});

	state.editItem = labelsWithValues;
	console.log(state.editItem);

	state.isDisabled = true;
	openDialog('详情', 1, '800px');
};

// 关闭弹框
const closeDialog = () => {
	state.dialogTitle = '';
	state.dialogType = 0;
	state.dialogVisible = false;
	state.editItem = {};
	state.isDisabled = false;
	searchForm();
};

// 分页改变
const onTablePageChange = (val: any) => {
	state.tableData.param.PageIndex = val.pageNum;
	state.tableData.param.PageSize = val.pageSize;
	getTableData();
};
// 搜索点击时表单回调
const searchForm = () => {
	state.tableData.param.PageIndex = 1;
	tableRef.value.pageReset();
};
// 重置
const resetForm = () => {
	listQuery.Name = '';
	listQuery.AppKey = '';
	listQuery.Status = '';
	listQuery.Address = '';
	state.dateArr = [];
	searchForm();
};
</script>

<style scoped lang="scss">
.system-role-container {
	.system-role-padding {
		padding: 15px;
		.el-table {
			flex: 1;
		}
	}
}
</style>
